package com.alibaba.citrus.cr.mn.order.center.facade.kafka;

import com.alibaba.cz.base.tool.log.Log;
import lombok.Data;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.StringSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * Chen.mu
 * 2023/4/23
 **/
@Data
@Component
@Configuration
public class KafkaProducerProperties {
    private static final Log log = Log.getLogger(KafkaProducerProperties.class);

    private static final String SERVERS = "alikafka-post-cn-uax36uo7c001-1-vpc.alikafka.aliyuncs.com:9092,alikafka-post-cn-uax36uo7c001-2-vpc.alikafka.aliyuncs.com:9092,alikafka-post-cn-uax36uo7c001-3-vpc.alikafka.aliyuncs.com:9092";
    private static final int RETRIES = 0;
    private static final int BATCH_SIZE = 4096;
    private static final int LINGER_MS = 1;
    private static final int BUFFER_MEMORY = 4096000;
    private static final int MAX_REQUEST_SIZE_CONFIG = 3000000;

    public Map<String, Object> producerConfigs() {
        Map<String, Object> props = new HashMap<>();
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, SERVERS);
        props.put(ProducerConfig.RETRIES_CONFIG, RETRIES);
        props.put(ProducerConfig.BATCH_SIZE_CONFIG, BATCH_SIZE);
        props.put(ProducerConfig.LINGER_MS_CONFIG, LINGER_MS);
        props.put(ProducerConfig.BUFFER_MEMORY_CONFIG, BUFFER_MEMORY);
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        props.put(ProducerConfig.MAX_REQUEST_SIZE_CONFIG, MAX_REQUEST_SIZE_CONFIG);
        return props;
    }

    public ProducerFactory<String, String> producerFactory() {
        return new DefaultKafkaProducerFactory<>(producerConfigs());
    }

    @Bean
    public KafkaTemplate<String, String> kafkaTemplate() {
        return new KafkaTemplate(producerFactory());
    }

}
